<!DOCTYPE html>






  


<html class="theme-next mist use-motion" lang="zh-CN">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2"/>
<meta name="theme-color" content="#222">












<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />






















<link href="/blog/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/blog/css/main.css?v=6.2.0" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/blog/images/apple-touch-icon-next.png?v=6.2.0">


  <link rel="icon" type="image/png" sizes="32x32" href="/blog/images/favicon-32x32-next.png?v=6.2.0">


  <link rel="icon" type="image/png" sizes="16x16" href="/blog/images/favicon-16x16-next.png?v=6.2.0">


  <link rel="mask-icon" href="/blog/images/logo.svg?v=6.2.0" color="#222">









<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/blog/',
    scheme: 'Mist',
    version: '6.2.0',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: false,
    fastclick: false,
    lazyload: false,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>


  




  <meta name="description" content="该内容为《轻松学算法 互联网算法面试宝典 ,赵烨》第1章读书笔记。对于一个大学没有学过《算法与数据结构》的老铁，希望通过这本书加深理解，拓展眼界，给自己的大学补补课。第1章里面举了 Java 的例子，正好我是 Java 后台开发，阅读不是很费力。">
<meta name="keywords" content="算法与数据结构">
<meta property="og:type" content="article">
<meta property="og:title" content="第 1 章 数组、集合和散列表">
<meta property="og:url" content="http://meixl.gitee.io/20180504/第1章_数组、集合和散列表/index.html">
<meta property="og:site_name" content="Hexo">
<meta property="og:description" content="该内容为《轻松学算法 互联网算法面试宝典 ,赵烨》第1章读书笔记。对于一个大学没有学过《算法与数据结构》的老铁，希望通过这本书加深理解，拓展眼界，给自己的大学补补课。第1章里面举了 Java 的例子，正好我是 Java 后台开发，阅读不是很费力。">
<meta property="og:locale" content="zh-CN">
<meta property="og:image" content="http://meixl.gitee.io/blog/images/pasted-3.png">
<meta property="og:updated_time" content="2018-05-07T13:33:34.197Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="第 1 章 数组、集合和散列表">
<meta name="twitter:description" content="该内容为《轻松学算法 互联网算法面试宝典 ,赵烨》第1章读书笔记。对于一个大学没有学过《算法与数据结构》的老铁，希望通过这本书加深理解，拓展眼界，给自己的大学补补课。第1章里面举了 Java 的例子，正好我是 Java 后台开发，阅读不是很费力。">
<meta name="twitter:image" content="http://meixl.gitee.io/blog/images/pasted-3.png">






  <link rel="canonical" href="http://meixl.gitee.io/20180504/第1章_数组、集合和散列表/"/>



<script type="text/javascript" id="page.configurations">
  CONFIG.page = {
    sidebar: "",
  };
</script>

  <title>第 1 章 数组、集合和散列表 | Hexo</title>
  









  <noscript>
  <style type="text/css">
    .use-motion .motion-element,
    .use-motion .brand,
    .use-motion .menu-item,
    .sidebar-inner,
    .use-motion .post-block,
    .use-motion .pagination,
    .use-motion .comments,
    .use-motion .post-header,
    .use-motion .post-body,
    .use-motion .collection-title { opacity: initial; }

    .use-motion .logo,
    .use-motion .site-title,
    .use-motion .site-subtitle {
      opacity: initial;
      top: initial;
    }

    .use-motion {
      .logo-line-before i { left: initial; }
      .logo-line-after i { right: initial; }
    }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-CN">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/blog/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Hexo</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button aria-label="切换导航栏">
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>




<nav class="site-nav">
  
    <ul id="menu" class="menu">
      
        
        
        
          
          <li class="menu-item menu-item-home">
    <a href="/blog/" rel="section">
      <i class="menu-item-icon fa fa-fw fa-home"></i> <br />首页</a>
  </li>
        
        
        
          
          <li class="menu-item menu-item-tags">
    <a href="/blog/tags" rel="section">
      <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />标签</a>
  </li>
        
        
        
          
          <li class="menu-item menu-item-archives">
    <a href="/blog/archives/" rel="section">
      <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />归档</a>
  </li>
        
        
        
          
          <li class="menu-item menu-item-about">
    <a href="/blog/about/" rel="section">
      <i class="menu-item-icon fa fa-fw fa-user"></i> <br />关于</a>
  </li>

      
      
    </ul>
  

  
    

  

  
</nav>



  



</div>
    </header>

    


    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://meixl.gitee.io/blog/20180504/第1章_数组、集合和散列表/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="meixiaoliang">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/blog/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Hexo">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">第 1 章 数组、集合和散列表
              
            
          </h1>
        

        <div class="post-meta">

          <span class="post-time">

            
            
            

            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              

              
                
              

              <time title="创建时间：2018-05-04 20:45:00" itemprop="dateCreated datePublished" datetime="2018-05-04T20:45:00+08:00">2018-05-04</time>
            

            
              

              
                
                <span class="post-meta-divider">|</span>
                

                <span class="post-meta-item-icon">
                  <i class="fa fa-calendar-check-o"></i>
                </span>
                
                  <span class="post-meta-item-text">更新于</span>
                
                <time title="修改时间：2018-05-07 21:33:34" itemprop="dateModified" datetime="2018-05-07T21:33:34+08:00">2018-05-07</time>
              
            
          </span>
		  
		  <!-- 字数统计 -->
		  <span class="post-meta-divider">|</span>
		  <span class="post-meta-item-icon">
		    <i class="fa fa-file-word-o"></i>
		  </span>
		  <span class="post-meta-item-text">
		    字数统计 602 字
		  </span>

          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>该内容为《轻松学算法 互联网算法面试宝典 ,赵烨》第1章读书笔记。<br>对于一个大学没有学过《算法与数据结构》的老铁，希望通过这本书加深理解，拓展眼界，给自己的大学补补课。<br>第1章里面举了 <code>Java</code> 的例子，正好我是 <code>Java</code> 后台开发，阅读不是很费力。  </p>
<a id="more"></a>
<hr>
<h1 id="想要用就要提前想好的数据结构——数组"><a href="#想要用就要提前想好的数据结构——数组" class="headerlink" title="想要用就要提前想好的数据结构——数组"></a>想要用就要提前想好的数据结构——数组</h1><p><strong>数组的特点</strong>  </p>
<blockquote>
<ul>
<li>定长  </li>
<li>按顺序访问  </li>
</ul>
</blockquote>
<p><strong>数组的初始化及操作</strong>  </p>
<pre><code class="Java"><span class="comment">// 第一种 init 方法</span>
<span class="keyword">int</span> [] array1 = <span class="keyword">new</span> <span class="keyword">int</span>[<span class="number">10</span>];

<span class="comment">// 第二种 init 方法</span>
<span class="keyword">int</span> [] array2 = {<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>};

<span class="keyword">int</span> [] array3 = <span class="keyword">new</span> <span class="keyword">int</span>[<span class="number">3</span>];
array3[<span class="number">0</span>]=<span class="number">1</span>;
array3[<span class="number">1</span>]=<span class="number">2</span>;
</code></pre>
<h1 id="升级版数组——集合"><a href="#升级版数组——集合" class="headerlink" title="升级版数组——集合"></a>升级版数组——集合</h1><p>以 <code>ArrayList</code> 为例， <code>ArrayList</code> 底层为数组。<br>当 <code>ArrayList</code> 长度超过初始化长度，则会自动拓容。<br><img src="\blog\images\pasted-3.png" alt="upload successful"></p>
<p><strong>集合的特点</strong>  </p>
<blockquote>
<ul>
<li>变长:当长度不够时，生成一个更长的数组。将旧数组的内容复制到新数组中。</li>
<li>所以在正常情况下有 2 个系统开销：<br>  一个是数组总是比我们使用的长。所以存在空间浪费。<br>  另外个是数组的拓容，会比较消耗系统的性能。</li>
</ul>
</blockquote>
<h1 id="数组的其它应用——散列表"><a href="#数组的其它应用——散列表" class="headerlink" title="数组的其它应用——散列表"></a>数组的其它应用——散列表</h1><blockquote>
<p>   散列表，又叫哈希表，是能够通过给定的关键字的值直接访问到具体对应的值的一个数据结构。<br>    也就是说，把关键字映射到一个表中的位置来直接访问记录，以加快访问速度。<br>通过 key 访问一个映射表来得到 value 的地址, 这个映射表叫做<strong>散列函数</strong>或<strong>哈希函数</strong>，存放记录的数组叫做<strong>散列表</strong>。<br>然而不同的关键字 key 可能会有相同的 value ，这种情况叫做<strong>碰撞</strong>。<br>散列表的 key 分布套路及碰撞解决套路，不表。  </p>
</blockquote>
<p><strong>散列表 2 种用法</strong>：<br>key 与 value 一致—— <code>Set</code><br>key 与 value 不一致—— <code>Map</code>  </p>
<p><strong>散列表的特点</strong></p>
<blockquote>
<ul>
<li>访问速度快</li>
<li>需要额外空间：散列表基本存不满。</li>
<li>无序</li>
<li>可能会产生碰撞</li>
</ul>
</blockquote>
<p><strong>散列表适用场景</strong></p>
<blockquote>
<ul>
<li>缓存</li>
<li>快速查找</li>
</ul>
</blockquote>
<p><strong>性能</strong><br>无碰撞O(1)，有碰撞O(L)，L为链表长度。</p>
<p><code>Java</code> 扩容因子默认 0.75，也可以自定义。<br>例如存储结构总长度为 8，扩容因子为 0.75 时，长度为 6 时会进行一次扩容。</p>
<hr>
<p>终于写完了。睡觉去。</p>

      
    </div>

    

    
    
    

    

    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/blog/tags/算法与数据结构/" rel="tag"># 算法与数据结构</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/blog/20180506/第2章_栈、队列、链表/" rel="prev" title="第 2 章 栈、队列、链表">
                第 2 章 栈、队列、链表 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <p class="site-author-name" itemprop="name">meixiaoliang</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          
            <nav class="site-state motion-element">
              
                <div class="site-state-item site-state-posts">
                
                  <a href="/blog/archives/">
                
                    <span class="site-state-item-count">3</span>
                    <span class="site-state-item-name">日志</span>
                  </a>
                </div>
              

              

              
                
                
                <div class="site-state-item site-state-tags">
                  <a href="/blog/tags/index.html">
                    
                    
                      
                    
                    <span class="site-state-item-count">1</span>
                    <span class="site-state-item-name">标签</span>
                  </a>
                </div>
              
            </nav>
          

          

          

          
          

          
          

          
            
          
          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#想要用就要提前想好的数据结构——数组"><span class="nav-number">1.</span> <span class="nav-text">想要用就要提前想好的数据结构——数组</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#升级版数组——集合"><span class="nav-number">2.</span> <span class="nav-text">升级版数组——集合</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#数组的其它应用——散列表"><span class="nav-number">3.</span> <span class="nav-text">数组的其它应用——散列表</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; 2018 &mdash; <span itemprop="copyrightYear">2019</span>
  <span class="with-love" id="animate">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">meixiaoliang</span>

  

  
</div>




  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动 v3.7.1</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/theme-next/hexo-theme-next">NexT.Mist</a> v6.2.0</div>




        








        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>


























  
  
    <script type="text/javascript" src="/blog/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  


  


  <script type="text/javascript" src="/blog/js/src/utils.js?v=6.2.0"></script>

  <script type="text/javascript" src="/blog/js/src/motion.js?v=6.2.0"></script>



  
  

  
  <script type="text/javascript" src="/blog/js/src/scrollspy.js?v=6.2.0"></script>
<script type="text/javascript" src="/blog/js/src/post-details.js?v=6.2.0"></script>



  


  <script type="text/javascript" src="/blog/js/src/bootstrap.js?v=6.2.0"></script>



  



	





  





  










  





  

  

  

  

  
  

  

  

  

  

  

</body>
</html>
